package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.incar.chuxing.R;
import com.incar.chuxing.adapter.DiscountAdapter;
import com.incar.chuxing.api.UrlConstant;
import com.incar.chuxing.baiduMap.GeoCoderUtils;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.SettlementDetailBean;
import com.incar.chuxing.utils.Constants;
import com.incar.chuxing.utils.IntentKey;
import com.incar.chuxing.utils.TimeUtils;
import com.incar.chuxing.widget.MyListView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PayDetailActivity extends BaseActivity {

    @BindView(R.id.title_name)
    TextView titleName;
    @BindView(R.id.progressBar_car_settlement)
    RelativeLayout progressBarCarSettlement;
    @BindView(R.id.order_car_return)
    ImageView orderCarReturn;
    @BindView(R.id.order_toolbar_car)
    RelativeLayout orderToolbarCar;
    @BindView(R.id.order_car_id)
    TextView orderCarId;
    @BindView(R.id.order_car_brand)
    TextView orderCarBrand;
    @BindView(R.id.order_car_paizhao)
    TextView orderCarPaizhao;
    @BindView(R.id.order_car_time0)
    TextView orderCarTime0;
    @BindView(R.id.order_car_tv_haoshi)
    TextView orderCarTvHaoshi;
    @BindView(R.id.order_car_rl_haoshi)
    RelativeLayout orderCarRlHaoshi;
    @BindView(R.id.order_car_orderTime)
    TextView orderCarOrderTime;
    @BindView(R.id.order_car_startTime)
    TextView orderCarStartTime;
    @BindView(R.id.order_car_startLocation)
    TextView orderCarStartLocation;
    @BindView(R.id.order_car_endTime)
    TextView orderCarEndTime;
    @BindView(R.id.order_car_endLocation)
    TextView orderCarEndLocation;
    @BindView(R.id.order_car_ll_haoshi)
    LinearLayout orderCarLlHaoshi;
    @BindView(R.id.order_car_money)
    TextView orderCarMoney;
    @BindView(R.id.order_car_yhmoney)
    TextView orderCarYhmoney;
    @BindView(R.id.order_car_yh_rl)
    RelativeLayout orderCarYhRl;
    @BindView(R.id.order_car_coupon)
    TextView orderCarCoupon;
    @BindView(R.id.order_car_tv_coupon)
    TextView orderCarTvCoupon;
    @BindView(R.id.order_car_sv)
    ScrollView orderCarSv;
    @BindView(R.id.textView1_settlement)
    TextView textView1Settlement;
    @BindView(R.id.order_car_payMoney)
    TextView orderCarPayMoney;
    @BindView(R.id.order_car_coupon_rl)
    RelativeLayout orderCarCouponRl;
    @BindView(R.id.order_car_bjmp)
    TextView orderCarBjmp;
    @BindView(R.id.order_car_bjmpmoney)
    TextView orderCarBjmpmoney;
    @BindView(R.id.order_car_rsbz)
    TextView orderCarRsbz;
    @BindView(R.id.order_car_rsbzmoney)
    TextView orderCarRsbzmoney;
    @BindView(R.id.order_car_rsbz_rl)
    RelativeLayout orderCarRsbzRl;
    @BindView(R.id.order_car_rule)
    TextView orderCarRule;
    @BindView(R.id.order_car_yh_line)
    View orderCarYhLine;
    @BindView(R.id.order_car_yh_block)
    View orderCarYhBlock;
    @BindView(R.id.order_car_coupon_block)
    View orderCarCouponBlock;

    @BindView(R.id.order_car_yh_lv)
    MyListView orderCarYhLv;
    @BindView(R.id.car_settlement_orderpay)
    TextView tvBottomOrderPay;
    @BindView(R.id.car_settlement_yhpay)
    TextView tvBottomYhPay;
    @BindView(R.id.title_more)
    ImageView titleMore;
    @BindView(R.id.title_tv_more)
    TextView titleTvMore;
    @BindView(R.id.order_car_qbjmoney)
    TextView orderCarQbjmoney;
    @BindView(R.id.order_car_csfmoney)
    TextView orderCarCsfmoney;
    @BindView(R.id.order_car_lcmoney)
    TextView orderCarLcmoney;
    @BindView(R.id.order_car_hcfjmoney)
    TextView orderCarHcfjmoney;
    private Context mContext = this;
    private String orderId = "";
    private String couponId = "0";
    private double couponPrice = 0;//优惠券的金额
    private String couponTitle;
    private double payMoney;
    private boolean isEnterprise;
    private int[] arrImgIds;
    private List<Map<String, Object>> list = new ArrayList<>();
    private String enterBalance;
    private String accountBalance;
    private String paymentMethod = "1";
    private double resultMoney;
    private int couponNum = 0;

    private int couponPay;
    private String reason;

    private int flag1 = 0;
    private int flag = 0;
    private DecimalFormat df = new DecimalFormat("0.00");

    @Override
    public int getLayoutId() {
        return R.layout.activity_car_settlement_detail;
    }

    @Override
    public void initView() {
        titleName.setText("订单结算详情");
        titleMore.setVisibility(View.GONE);
        titleTvMore.setVisibility(View.VISIBLE);
        titleTvMore.setText("计价规则");
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        orderId = intent.getStringExtra(IntentKey.INTENT_ORDER_ID);
        couponPrice = intent.getDoubleExtra(IntentKey.INTENT_COUPONPRICE, 0);
        couponTitle = intent.getStringExtra(IntentKey.INTENT_COUPONTITLE);
        if (couponPrice <= 0) {
            orderCarTvCoupon.setText("未使用优惠券");
        } else {
            orderCarTvCoupon.setText(couponPrice + "元" + couponTitle);
        }
        getSettlementDetail();
    }


    private void getSettlementDetail() {
        SettlementDetailBean settlementDetailBean = (SettlementDetailBean) getIntent().getSerializableExtra(IntentKey.INTENT_SETTLEMENT_CAR_DETAIL);
        SettlementDetailBean.AttachmentBean result = settlementDetailBean.getAttachment();
        orderCarId.setText(result.getModel().getOrderNum());
        orderCarBrand.setText(result.getModel().getCarBrandModel());
        orderCarPaizhao.setText(result.getModel().getLicenseTag());
        int totalMin = (int) (result.getModel().getDayMinutes() + result.getModel().getNightMinutes() + result.getModel().getTimeoutMinutes());
        orderCarTvHaoshi.setText(TimeUtils.formatMin(totalMin * 60 + ""));
        orderCarOrderTime.setText(TimeUtils.getFormatDate(result.getModel().getOrderTime() + "", "yyyy-MM-dd HH:mm:ss"));
        orderCarStartTime.setText(TimeUtils.getFormatDate(result.getModel().getStartDate() + "", "yyyy-MM-dd HH:mm:ss"));
        orderCarEndTime.setText(TimeUtils.getFormatDate(result.getModel().getEndDate() + "", "yyyy-MM-dd HH:mm:ss"));
        orderCarStartLocation.setText(result.getModel().getOnLocationName());
        orderCarEndLocation.setText(result.getModel().getDownLocationName());

        //订单费用
        DecimalFormat df = new DecimalFormat("0.00");
        /*orderCarMoney.setText(df.format(result.getModel().getDayMinutes() * result.getModel().getDayUnitPrice()
                + result.getModel().getNightMinutes() * result.getModel().getNightUnitPrice() +
                result.getModel().getTimeoutMoney() + result.getModel().getInsuranceMoney()) + "元");*/
        orderCarMoney.setText(df.format(result.getModel().getTotalMoney())+ "元");
        /*tvBottomOrderPay.setText("订单￥" + df.format(result.getModel().getDayMinutes() * result.getModel().getDayUnitPrice()
                + result.getModel().getNightMinutes() * result.getModel().getNightUnitPrice() +
                result.getModel().getTimeoutMoney() + result.getModel().getInsuranceMoney()));*/
        tvBottomOrderPay.setText("订单￥" + df.format(result.getModel().getOrderPrice()));
       /* //日间费用
        String rj = df.format(result.getModel().getDayMinutes() * result.getModel().getDayUnitPrice());
        if (rj.equals("0.00")) {
            orderCarRjRl.setVisibility(View.GONE);
            orderCarRjLine.setVisibility(View.GONE);
        } else {
            orderCarRjRl.setVisibility(View.VISIBLE);
            orderCarRjLine.setVisibility(View.VISIBLE);
            orderCarRjDj.setText("日间费用：" + df.format(result.getModel().getDayUnitPrice()) + "元/分钟");
            orderCarRjmoney.setText(rj + "元");
        }*/
        //夜间费用
     /*   String wj = df.format(result.getModel().getNightMinutes() * result.getModel().getNightUnitPrice());
        if (wj.equals("0.00")) {
            orderCarWjRl.setVisibility(View.GONE);
            orderCarWjLine.setVisibility(View.GONE);
        } else {
            orderCarWjRl.setVisibility(View.VISIBLE);
            orderCarWjLine.setVisibility(View.VISIBLE);
            orderCarWjDj.setText("晚间费用：" + df.format(result.getModel().getNightUnitPrice()) + "元/分钟");
            orderCarWjmoney.setText(wj + "元");
        }*/
        orderCarQbjmoney.setText(df.format(result.getModel().getStartingPrice()) + "元");

        orderCarCsfmoney.setText(df.format(result.getModel().getDayMoney()) + "元");

        orderCarLcmoney.setText(df.format(result.getModel().getMileagePrice()) + "元");

        orderCarHcfjmoney.setText(df.format(result.getModel().getDispatchingFee()) + "元");

        orderCarBjmpmoney.setText(df.format(result.getModel().getAbatement()) + "元");
        orderCarRsbzmoney.setText(df.format(result.getModel().getCompanyByRisk()) + "元");
        //优惠费用
        if (result.getModel().getSpecialModelList() != null) {
            DiscountAdapter adapter = new DiscountAdapter(result.getModel().getSpecialModelList(), mContext);
            orderCarYhLv.setAdapter(adapter);
        }
        tvBottomYhPay.setText("优惠￥" + (df.format(result.getModel().getTotalSubMoney() + couponPrice)));
        orderCarYhmoney.setText((df.format(result.getModel().getTotalSubMoney() + couponPrice)) + "元");

        payMoney = result.getModel().getTotalMoney();
        resultMoney = payMoney;
        //最终支付金额
        double realPrice = resultMoney - couponPrice;
        orderCarPayMoney.setText("￥" + df.format(realPrice < 0 ? 0 : realPrice));
        Constants.orderPrice = df.format(resultMoney); //邀请好友界面使用结算价格

        //能不能使用优惠券
        couponPay = (int) result.getModel().getCouponPay();
        if (result.getModel().getCouponPay() == 0) {
            couponNum = 0;
            reason = result.getModel().getCouponForbidReason();
            orderCarCouponRl.setVisibility(View.VISIBLE);
            orderCarCouponBlock.setVisibility(View.VISIBLE);
            orderCarCouponRl.setClickable(false);
            orderCarTvCoupon.setText(result.getModel().getCouponForbidReason());
        } else if (result.getModel().getCouponPay() == 1) {
            //没有优惠券，隐藏优惠券框
            if (result.getCoupons() == null || result.getCoupons().size() == 0) {
                couponNum = 0;
                orderCarCouponRl.setVisibility(View.GONE);
                orderCarCouponBlock.setVisibility(View.GONE);
            } else {
                couponNum = result.getCoupons().size();
                orderCarCouponRl.setVisibility(View.VISIBLE);
                orderCarCouponBlock.setVisibility(View.VISIBLE);
            }
            //orderCarTvCoupon.setText(couponNum + "张可用");
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        progressBarCarSettlement.setVisibility(View.GONE);
    }

    @OnClick({R.id.order_car_rule, R.id.title_back, R.id.title_tv_more,
            R.id.order_car_yh_rl})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.order_car_coupon_rl:
                Intent intent = new Intent(mContext, ChooseCouponActivity.class);
                intent.putExtra(IntentKey.INTENT_ORDER_ID, orderId);
                intent.putExtra(IntentKey.INTENT_TRIP_MODE, "1");
                startActivityForResult(intent, Constants.REQUEST_CHOOSE_COUPON);
                break;
            case R.id.title_tv_more:
                Intent intent2 = new Intent();
                intent2.setClass(mContext, WebViewActivity.class);
                intent2.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, "计价规则");
                intent2.putExtra(IntentKey.INTENT_WEBVIEW_URL, UrlConstant.Request.VALUE_RULES);
                startActivity(intent2);
                break;
        }
    }


}
